﻿// Decompiled with JetBrains decompiler
// Type: MG_Cooler.HeaterLogicTemperatureSensor
// Assembly: MG_Cooler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 7560BFF2-5E64-4596-AB14-3DB707CE39FE
// Assembly location: E:\docs\Klei\OxygenNotIncluded\mods\Steam\2573951847\MG_Cooler.dll

using KSerialization;
using STRINGS;
using UnityEngine;

namespace MG_Cooler
{
  [SerializationConfig(MemberSerialization.OptIn)]
  public class HeaterLogicTemperatureSensor : Switch, ISaveLoadable, IThresholdSwitch, ISim200ms
  {
    private const int NumFrameDelay = 8;
    private HandleVector<int>.Handle structureTemperature;
    private int simUpdateCounter;
    [Serialize]
    public float thresholdTemperature = 300f;
    [Serialize]
    public bool activateOnWarmerThan = true;
    [Serialize]
    private bool dirty = true;
    public float minTemp;
    public float maxTemp = 800.15f;
    private float[] temperatures = new float[8];
    private float averageTemp;
    private bool wasOn;
    [MyCmpAdd]
    private CopyBuildingSettings copyBuildingSettings;
    private static readonly EventSystem.IntraObjectHandler<HeaterLogicTemperatureSensor> OnCopySettingsDelegate = new EventSystem.IntraObjectHandler<HeaterLogicTemperatureSensor>((System.Action<HeaterLogicTemperatureSensor, object>) ((component, data) => component.OnCopySettings(data)));

    public float StructureTemperature => GameComps.StructureTemperatures.GetPayload(this.structureTemperature).Temperature;

    protected override void OnPrefabInit()
    {
      base.OnPrefabInit();
      this.Subscribe<HeaterLogicTemperatureSensor>(-905833192, HeaterLogicTemperatureSensor.OnCopySettingsDelegate);
    }

    private void OnCopySettings(object data)
    {
      HeaterLogicTemperatureSensor component = ((GameObject) data).GetComponent<HeaterLogicTemperatureSensor>();
      if (!((UnityEngine.Object) component != (UnityEngine.Object) null))
        return;
      this.Threshold = component.Threshold;
      this.ActivateAboveThreshold = component.ActivateAboveThreshold;
    }

    protected override void OnSpawn()
    {
      base.OnSpawn();
      this.structureTemperature = GameComps.StructureTemperatures.GetHandle(this.gameObject);
      this.OnToggle += new System.Action<bool>(this.OnSwitchToggled);
      this.UpdateVisualState(true);
      this.UpdateLogicCircuit();
      this.wasOn = this.switchedOn;
    }

    public void Sim200ms(float dt)
    {
      if (this.simUpdateCounter < 8 && !this.dirty)
      {
        int cell = Grid.PosToCell((KMonoBehaviour) this);
        if ((double) Grid.Mass[cell] <= 0.0)
          return;
        this.temperatures[this.simUpdateCounter] = Grid.Temperature[cell];
        ++this.simUpdateCounter;
      }
      else
      {
        this.simUpdateCounter = 0;
        this.dirty = false;
        this.averageTemp = 0.0f;
        for (int index = 0; index < 8; ++index)
          this.averageTemp += this.temperatures[index];
        this.averageTemp /= 8f;
        if (this.activateOnWarmerThan)
        {
          if (((double) this.averageTemp <= (double) this.thresholdTemperature || this.IsSwitchedOn) && ((double) this.averageTemp > (double) this.thresholdTemperature || !this.IsSwitchedOn))
            return;
          this.Toggle();
        }
        else
        {
          if (((double) this.averageTemp < (double) this.thresholdTemperature || !this.IsSwitchedOn) && ((double) this.averageTemp >= (double) this.thresholdTemperature || this.IsSwitchedOn))
            return;
          this.Toggle();
        }
      }
    }

    public float GetTemperature() => this.averageTemp;

    private void OnSwitchToggled(bool toggled_on)
    {
      this.UpdateVisualState();
      this.UpdateLogicCircuit();
    }

    private void UpdateLogicCircuit() => this.GetComponent<LogicOperationalController>().operational.SetFlag(LogicOperationalController.LogicOperationalFlag, this.IsSwitchedOn);

    private void UpdateVisualState(bool force = false)
    {
      if (this.wasOn == this.switchedOn && !force)
        return;
      this.wasOn = this.switchedOn;
      KBatchedAnimController component = this.GetComponent<KBatchedAnimController>();
      component.Play((HashedString) (this.switchedOn ? "on_pre" : "on_pst"));
      component.Queue((HashedString) (this.switchedOn ? "on" : "off"));
    }

    protected override void UpdateSwitchStatus()
    {
    }

    public float Threshold
    {
      get => this.thresholdTemperature;
      set
      {
        this.thresholdTemperature = value;
        this.dirty = true;
      }
    }

    public bool ActivateAboveThreshold
    {
      get => this.activateOnWarmerThan;
      set
      {
        this.activateOnWarmerThan = value;
        this.dirty = true;
      }
    }

    public float CurrentValue => this.GetTemperature();

    public float RangeMin => this.minTemp;

    public float RangeMax => this.maxTemp;

    public float GetRangeMinInputField() => GameUtil.GetConvertedTemperature(this.RangeMin);

    public float GetRangeMaxInputField() => GameUtil.GetConvertedTemperature(this.RangeMax);

    public LocString Title => UI.UISIDESCREENS.TEMPERATURESWITCHSIDESCREEN.TITLE;

    public LocString ThresholdValueName => UI.UISIDESCREENS.THRESHOLD_SWITCH_SIDESCREEN.TEMPERATURE;

    public string AboveToolTip => (string) UI.UISIDESCREENS.THRESHOLD_SWITCH_SIDESCREEN.TEMPERATURE_TOOLTIP_ABOVE;

    public string BelowToolTip => (string) UI.UISIDESCREENS.THRESHOLD_SWITCH_SIDESCREEN.TEMPERATURE_TOOLTIP_BELOW;

    public string Format(float value, bool units) => GameUtil.GetFormattedTemperature(value, displayUnits: units, roundInDestinationFormat: true);

    public float ProcessedSliderValue(float input) => Mathf.Round(input);

    public float ProcessedInputValue(float input) => GameUtil.GetTemperatureConvertedToKelvin(input);

    public LocString ThresholdValueUnits()
    {
      LocString locString = (LocString) null;
      switch (GameUtil.temperatureUnit)
      {
        case GameUtil.TemperatureUnit.Celsius:
          locString = UI.UNITSUFFIXES.TEMPERATURE.CELSIUS;
          break;
        case GameUtil.TemperatureUnit.Fahrenheit:
          locString = UI.UNITSUFFIXES.TEMPERATURE.FAHRENHEIT;
          break;
        case GameUtil.TemperatureUnit.Kelvin:
          locString = UI.UNITSUFFIXES.TEMPERATURE.KELVIN;
          break;
      }
      return locString;
    }

    public ThresholdScreenLayoutType LayoutType => ThresholdScreenLayoutType.SliderBar;

    public int IncrementScale => 1;

    public NonLinearSlider.Range[] GetRanges => new NonLinearSlider.Range[4]
    {
      new NonLinearSlider.Range(25f, 260f),
      new NonLinearSlider.Range(50f, 400f),
      new NonLinearSlider.Range(12f, 1500f),
      new NonLinearSlider.Range(13f, 10000f)
    };
  }
}
